package com.example.clock;

import android.app.Activity;
import android.app.AlarmManager;
import android.app.AlertDialog;
import android.app.PendingIntent;
import android.app.TimePickerDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.Toast;

import java.util.Calendar;
public class MainActivity extends Activity {
   private TextView textView;
   private EditText editText;
   private Button setbtn,unsetbtn;
   //重复闹钟的按钮和TextView   
   private Button setbtns,unsetbtns;
   private TextView textView1;
   private static final int ButtonAlarm =1;
   //铃声文件夹
   private String setAlarmFloder = "/sdcard/music/alarms";

   private static final String TEMP_1="temp_1";
   Calendar c = Calendar.getInstance();
   private static int timeID= 0;
   public static String myAlert;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        textView = (TextView)findViewById(R.id.textview1);
        setbtn = (Button)findViewById(R.id.setclock);
        unsetbtn = (Button)findViewById(R.id.unsetclock);
		editText = (EditText)findViewById(R.id.editText);
        
        //获取SharedPreferences
        SharedPreferences per = getSharedPreferences(TEMP_1, MODE_WORLD_READABLE);
        //得到text内容
        String text_1 =per.getString("text_1", "当前没有设置提醒时间!");
		 myAlert = per.getString("alert","");
        //在text中显示内容
        textView.setText(text_1);
		editText.setText(myAlert);

        
        setbtn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				myAlert=editText.getText().toString();
				// 取得按下按钮的时间作为TimePickerDialog的默认值
				c.setTimeInMillis(System.currentTimeMillis());
				//定义获取时间
				int mHour = c.get(Calendar.HOUR_OF_DAY);
				int mMinute = c.get(Calendar.MINUTE);
				//跳出TimePickerDialog来设定时间
				new TimePickerDialog(MainActivity.this,
						new TimePickerDialog.OnTimeSetListener() {
							
							@Override
							public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
								// 取得设定后的时间 秒跟毫秒设为0
								c.setTimeInMillis(System.currentTimeMillis());
								c.set(Calendar.HOUR_OF_DAY, hourOfDay);
								c.set(Calendar.MINUTE, minute);
								c.set(Calendar.SECOND, 0);
								c.set(Calendar.MILLISECOND, 0);
								//定闹钟设定时间到时要执行CallAlarm.class
								Intent intent  = new Intent(MainActivity.this,CallAlarm.class);
								PendingIntent sender = PendingIntent.getBroadcast(MainActivity.this, 1, intent, PendingIntent.FLAG_UPDATE_CURRENT);
								//获取AlarmManager 
								//AlarmManager.RTC_WAKEUP设定服务在系统休眠时同样会执行
								//
								AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);
								//set()设定的PendingIntent只会执行一次
								am.set(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(),sender);
								//显示设定的闹钟时间
								String tmpS = format(hourOfDay)+":"+ format(minute);
								textView.setText(tmpS);
								//toas显示设定的时间
								Toast.makeText(MainActivity.this, "设定的时间为:"+tmpS, Toast.LENGTH_LONG).show();
							}
						},mHour, mMinute, true).show();
				}
		});
        //取消闹钟
        
        unsetbtn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent intent = new  Intent (MainActivity.this,CallAlarm.class);
				PendingIntent sender = PendingIntent.getBroadcast(MainActivity.this, 0, intent, 0);
				//由AlarmManager中移除
				AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);
				am.cancel(sender);
				textView.setText("当前没有设置提醒!");
				//以Toast提示已删除设定，并更新显示的闹钟时间
				Toast.makeText(MainActivity.this, "提醒已取消",Toast.LENGTH_LONG).show();
			}
		});

    }
	private String format(int x)
    {
      String s = "" + x;
      if (s.length() == 1)
        s = "0" + s;
      return s;
    }
	@Override
	protected void onStop() {
		// TODO Auto-generated method stub
		super.onStop();
		//获取编译器
		SharedPreferences.Editor editor = 
				getSharedPreferences(TEMP_1, MODE_WORLD_WRITEABLE).edit();
		//将text内容添加到编译器 
		editor.putString("text_1", textView.getText().toString());
		editor.putString("alert",editText.getText().toString());
		//提交编辑内容
		editor.commit();
	}
}